/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import static clearsyntax.base.Range;
import static clearsyntax.collection.immutable.SortedMap;
import static clearsyntax.simple._;
import static java.lang.System.out;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * TODO make doc
 * 
 * @author Jonhnny Weslley
 * @version 1.00, 20/04/2008
 * @since 1.0
 */
public class PbJug {

	private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat(
			"dd/MM/yyyy");

	public static Date Date(String date) {
		try {
			return SIMPLE_DATE_FORMAT.parse(date);
		} catch (ParseException e) {
			throw new IllegalArgumentException(e);
		}
	}

	public static SortedMap<Date, String> formatTimeSerieToHourlyExport(
			SortedMap<Date, String> timeSerie, Date initialDate, Date finalDate) {

		List<Date> newDates = new ArrayList<Date>();
		newDates.add(initialDate);

		Calendar cal = Calendar.getInstance();
		cal.setTime(initialDate);
		for (Date date = cal.getTime(); date.compareTo(finalDate) < 0; date = cal.getTime()) {
			cal.add(Calendar.HOUR_OF_DAY,1);
			newDates.add(date);
		}

		String value;
		Set<Date> dates = timeSerie.keySet();
		SortedMap<Date, String> result = new TreeMap<Date, String>();
		for (Date date : newDates) {
			value = dates.contains(date) ? timeSerie.get(date) : "-99";
			result.put(date, value);
		}
		return result;
	}

	public static SortedMap<Date, String> formatTimeSerieToHourlyExportWithClearSyntax(
			SortedMap<Date, String> timeSerie, Date initialDate, Date finalDate) {

		List<Date> newDates = new ArrayList<Date>();
		newDates.add(initialDate);

		for (Date date : Range(initialDate, finalDate, Calendar.HOUR_OF_DAY)) {
			newDates.add(date);
		}

		String value;
		Set<Date> dates = timeSerie.keySet();
		SortedMap<Date, String> result = new TreeMap<Date, String>();
		for (Date date : newDates) {
			value = dates.contains(date) ? timeSerie.get(date) : "-99";
			result.put(date, value);
		}
		return result;
	}

	public static void main(String[] args) {
		Date start = Date("19/04/2008");
		Date end = new Date(); // Date("23/04/2008");

		SortedMap<Date, String> mapi = SortedMap(_(Date("16/04/2008"), "a"), _(
				Date("21/04/2008"), "b"), _(Date("01/05/2008"), "c"), _(
				Date("05/05/2008"), "d"));

		SortedMap<?, ?> map = formatTimeSerieToHourlyExportWithClearSyntax(mapi, start, end);
		out.println(map.size());
		Set<?> keySet = map.keySet();
		for (Object object : keySet) {
			System.out.println(object);
		}
	}

}
